<template>
  <div class="admin-edit">
    <div class="edit-form">
      <h2>编辑网站信息</h2>
      <form @submit.prevent="saveChanges">
        <div class="form-group">
          <label for="name">网站名称:</label>
          <input 
            type="text" 
            id="name" 
            v-model="editedWebsite.name" 
            required
          />
        </div>
        
        <div class="form-group">
          <label for="url">网站链接:</label>
          <input 
            type="text" 
            id="url" 
            v-model="editedWebsite.url" 
          />
        </div>
        
        <div class="form-group">
          <label for="description">内容描述:</label>
          <textarea 
            id="description" 
            v-model="editedWebsite.description" 
            required
          ></textarea>
        </div>
        
        <div class="form-group">
          <label for="category">分类:</label>
          <input 
            type="text" 
            id="category" 
            v-model="editedWebsite.category" 
            required
          />
        </div>
        
        <div class="form-group">
          <label for="createdAt">创建时间:</label>
          <input 
            type="text" 
            id="createdAt" 
            v-model="editedWebsite.createdAt" 
            required
          />
        </div>
        
        <div class="form-actions">
          <button type="submit">保存</button>
          <button type="button" @click="cancel">取消</button>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter, useRoute } from 'vue-router'
import dbService from '../services/indexedDB.js'

export default {
  name: 'AdminEdit',
  setup() {
    const store = useStore()
    const router = useRouter()
    const route = useRoute()
    
    const editedWebsite = ref({
      id: null,
      name: '',
      url: '',
      description: '',
      category: '',
      createdAt: ''
    })
    
    const loadWebsite = async () => {
      try {
        const website = await dbService.getWebsiteById(route.params.id)
        if (website) {
          editedWebsite.value = { ...website }
        } else {
          router.push('/') // 如果找不到网站，返回主页
        }
      } catch (error) {
        console.error('获取网站信息失败:', error)
        router.push('/')
      }
    }
    
    const saveChanges = async () => {
      try {
        await store.dispatch('updateWebsite', editedWebsite.value)
        router.push('/')
      } catch (error) {
        console.error('保存网站信息失败:', error)
        alert('保存失败，请重试')
      }
    }
    
    const cancel = () => {
      router.push('/')
    }
    
    onMounted(() => {
      loadWebsite()
    })
    
    return {
      editedWebsite,
      saveChanges,
      cancel
    }
  }
}
</script>

<style scoped>
.admin-edit {
  padding: 2rem;
  background-color: var(--background-color);
  min-height: 100vh;
}

.edit-form {
  background: var(--card-background);
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  max-width: 600px;
  margin: 0 auto;
}

.edit-form h2 {
  margin-top: 0;
  color: var(--text-color);
}

.form-group {
  margin-bottom: 1rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: bold;
  color: var(--text-color);
}

input, textarea {
  width: 100%;
  padding: 0.5rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-sizing: border-box;
  background-color: var(--card-background);
  color: var(--text-color);
}

textarea {
  min-height: 100px;
  resize: vertical;
}

.form-actions {
  display: flex;
  gap: 1rem;
  margin-top: 2rem;
}

button {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
}

button[type="submit"] {
  background-color: #28a745;
  color: white;
}

button[type="submit"]:hover {
  background-color: #218838;
}

button[type="button"] {
  background-color: #6c757d;
  color: white;
}

button[type="button"]:hover {
  background-color: #5a6268;
}

@media (max-width: 768px) {
  .admin-edit {
    padding: 1rem;
  }
  
  .edit-form {
    padding: 1rem;
  }
}
</style>